package web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Game;
import model.Hand;
import model.Player;
import model.Pot;
import model.Round;

/**
 * Servlet implementation class RefreshSeatsServlet
 */
@WebServlet("/RefreshSeatsServlet")
public class RefreshSeatsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public RefreshSeatsServlet() {
        super();
        // TODO Auto-generated constructor stub
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		//Setup response to XML
		response.setContentType("text/xml");
		response.setCharacterEncoding("UTF-8");
		response.setHeader( "Cache-Control", "no-cache" );
		
		HttpSession session = request.getSession(true);
		ServletContext sc = getServletContext();
		Player[] playerList = (Player[]) sc.getAttribute("playerList");
		Game game = (Game) sc.getAttribute("game");
		Round round = (Round) sc.getAttribute("round");
		Pot pot = (Pot) sc.getAttribute("pot");
		int playerCount = 0;
		String output = "<response>";
				
		for(int x=0; x<9; x++){
			if(playerList[x]!=null){
				playerCount++;
				output += "<player>";
				
				if(playerList[x].getSessionId() == session.getId()){
					output += "<type>2</type>";
				}
				else{
					output += "<type>1</type>";
				}
				
				output = output + "<name>" + playerList[x].getName() + "</name>";
				if(round!=null && round.isStarted()){	//can be fixed
					if(round.isStarted()){
						output += "<blinds>" + playerList[x].getBlindType() + "</blinds>";
						output += "<money>" + playerList[x].getMoney() + "</money>";
						output += "<bet>" + playerList[x].getBet() + "</bet>";
						Hand playerHand = playerList[x].getHand();
						output += "<card1>" + playerHand.getCardList().get(0).getImageName() + "</card1>";
						output += "<card2>" + playerHand.getCardList().get(1).getImageName() + "</card2>";	
						if(playerList[x].isTurn()){
							output += "<isTurn>true</isTurn>";
							if(playerList[x].isCanFold())
								output += "<canAct>true</canAct>";
							else
								output += "<canAct>false</canAct>";
							
							if(playerList[x].isCanCall())
								output += "<canAct>true</canAct>";
							else
								output += "<canAct>false</canAct>";
							
							if(playerList[x].isCanCheck())
								output += "<canAct>true</canAct>";
							else
								output += "<canAct>false</canAct>";
							
							if(playerList[x].isCanRaise())
								output += "<canAct>true</canAct>";
							else
								output += "<canAct>false</canAct>";
						}	
						else
							output += "<isTurn>false</isTurn>";

						if(playerList[x].hasFolded())
							output += "<hasFolded>true</hasFolded>";
						else
							output += "<hasFolded>false</hasFolded>";
					}
				}
				
				else{
					output += "<blinds>not yet</blinds>";
					output += "<money>not yet</money>";
					output += "<bet>0</bet>";
					output += "<card1>NotYet.png</card1>";
					output += "<card2>NotYet.png</card2>";
					output += "<isTurn>false</isTurn>";
					output += "<hasFolded>false</hasFolded>";
				}
				
				output += "</player>";
			}
			else{
				output += "<player>";
				output += "<type>0</type>";
				output += "<name>none</name>";
				output += "<blinds>none</blinds>";
				output += "<money>none</money>";
				output += "<bet>none</bet>";
				output += "<card1>none</card1>";
				output += "<card2>none</card2>";
				output += "<isTurn>false</isTurn>";
				output += "<hasFolded>false</hasFolded>";
				output += "</player>";
			}
		}
		if(round!=null && round.isShowDown())
			output += "<showdown>true</showdown>";
		else
			output += "<showdown>false</showdown>";
		
		if(round!=null && round.isHasEnded())
			output += "<ended>true</ended>";
		else
			output += "<ended>false</ended>";
		
		output +="<playerCount>"+playerCount+"</playerCount>";
		
		if(game.isStarted()&&round!=null){
			output += "<gameStatus>true</gameStatus>";
			output += "<pot>" + pot.getAmount() + "</pot>";
			output += "<lastBet>" + round.getLastBet() + "</lastBet>";
			output +=  "<roundCardCount>"+round.getCardList().size()+"</roundCardCount>";
			if(round.getCardList().size()!=0){
				if(round.getCardList().size()>2){
					output += "<roundCards>"+round.getCardList().get(0).getImageName()+"</roundCards>";
					output += "<roundCards>"+round.getCardList().get(1).getImageName()+"</roundCards>";
					output += "<roundCards>"+round.getCardList().get(2).getImageName()+"</roundCards>";
				}
				if(round.getCardList().size()>3)
					output += "<roundCards>"+round.getCardList().get(3).getImageName()+"</roundCards>";
				if(round.getCardList().size()>4)
					output += "<roundCards>"+round.getCardList().get(4).getImageName()+"</roundCards>";
			}
		}
		else{
			output += "<gameStatus>false</gameStatus>";
		}
		
		output += "</response>";
		out.print(output);
	}
}
